<?php

namespace app\home\controller;

use app\home\model\pyg_user;
use app\home\validate\Checked;
use app\home\validate\User;
use app\Request;
use think\exception\ValidateException;
use think\facade\Session;
use think\facade\View;

class Login
{
    /**
     * @return string
     */
    public function register()
    {
        return View::fetch('login/register');
    }

    /**
     * @param Request $request
     * @return \think\response\Json
     */
    public function security(Request $request)
    {
        $phones = $request->post('phone');
        if(time()-cache('time') < 60)
        {
            return fail('请求频繁');
        }
        $rand = rand(1000,9999);
        $statusStr = array(
            "0" => "短信发送成功",
            "-1" => "参数不全",
            "-2" => "服务器空间不支持,请确认支持curl或者fsocket，联系您的空间商解决或者更换空间！",
            "30" => "密码错误",
            "40" => "账号不存在",
            "41" => "余额不足",
            "42" => "帐户已过期",
            "43" => "IP地址限制",
            "50" => "内容含有敏感词"
        );
        $smsapi = "http://api.smsbao.com/";
        //短信平台帐号
        $user = "xianhu";
        //短信平台密码
        $pass = md5("hu521314..");
        //要发送的短信内容
        $content="短信信息为$rand;时间为3分钟有效";
        //要发送短信的手机号码
        $sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phones."&c=".urlencode($content);
        $result =file_get_contents($sendurl);
        cache('rand',$rand,180);
        cache('time',time());
        return success(cache('rand'));
    }

    /**
     * @param Request $request
     * @return \think\response\Json
     */
    public function verify(Request $request)
    {
        $data = $request->all();
        if($data['password']!=$data['repassword'])
        {
            return fail('二次密码不一致');
        }
        if($data['code']!=cache('rand'))
        {
            return fail('验证码不正确');
        }
        try {
            validate(User::class)->check($data);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return json([
                'code'=>600,
                'msg'=>$e->getError(),
                'data'=>'',
            ]);
        }
        try {
            $data['password'] = md5(md5($data['password']));
            $data['nickname'] = substr_replace($data['phone'],'****',3,4);
            $obj = new pyg_user();
            $obj->addUser($data);
            return success('注册成功');
        }catch (\Exception $exception)
        {
            return fail($exception->getMessage());
        }
    }

    /**
     * @return string
     */
    public function text()
    {
        return View::fetch('login/login');
    }

    /**
     * @param Request $request
     * @return \think\response\Json
     */
    public function loginTo(Request $request)
    {
        $data = $request->post();
        try {
            validate(Checked::class)->check($data);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return fail($e->getMessage());
        }
        try {
            $obj = new pyg_user();
            $res = $obj->loginUser($data);
            if($res)
            {
                if($res['password']==md5(md5($data['password'])))
                {
                    Session::set('name',$res['nickname']);
                    Session::set('id',$res['id']);
                    return success('登录成功');
                }
                return fail('密码错误');
            }
            return  fail('账号错误');
        }catch (\Exception $exception)
        {
            return fail($exception->getMessage());
        }
    }

    /**
     * @return \think\response\Redirect
     */
    public function qqcallback()
    {
        require_once("./qq/API/qqConnectAPI.php");
        $qc = new \QC();
        $callback = $qc->qq_callback();
        $openid = $qc->get_openid();
        $qc = new \QC($callback,$openid);
        $info = $qc->get_user_info();
        $data = [
            'nickname' => $info['nickname'],
            'open_type' => 'qq',
            'openid' => $openid,
        ];
        $obj = new pyg_user();
        $res = $obj->showUser($openid);
        if($res)
        {
            $obj = new pyg_user();
            $obj->updates($data,$openid);
        }
        else
        {
            $obj = new pyg_user();
            $obj->addPin($info,$openid);
        }
        Session::set('name',$res['nickname']);
        Session::set('id',$res['id']);
        return  redirect('index');
    }

    /**
     * @return \think\response\Redirect
     */
    public function logout()
    {
        Session('name',NULL);
        return redirect('text');
    }
}